package com.gaswell.utils;

import org.springframework.test.context.jdbc.Sql;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName DateUtils
 * @Author xiaow
 * @DATE 2023/2/21 15:07
 **/

public class DateUtils {
//  秒
    public final static long SECOND = 1000;
//  分
    public final static long MINUTE = SECOND * 60;
//  时
    public final static long HOUR = MINUTE * 60;
//  天
    public final static long DAY = HOUR * 24;


    public static Timestamp stringToDate(String str1) {
        SimpleDateFormat sdf = null;
        if (str1.contains("-")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        }
        Date date1 = null;
        try {
            date1 = sdf.parse(str1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Timestamp timestamp = new Timestamp(date1.getTime());
        return timestamp;
    }

    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        return format;
    }

    public static String sqldateToString(java.sql.Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        return format;
    }


    public static java.sql.Date stringTosqldate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new java.sql.Date(parse.getTime());
    }

    public static java.sql.Date stringTosqldate2(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new java.sql.Date(parse.getTime());
    }

    public static String dateToString2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String format = sdf.format(date);
        return format;
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(stringTosqldate("2020-01-22"));

    }
}
